<template>
	<view class="content">
		<u--form labelPosition="left" :model="formData" :rules="rules" ref="form1">
			<u-form-item label="评论内容" prop="mark" borderBottom ref="item1">
				<u--textarea v-model="formData.mark" placeholder="请输入评论内容"></u--textarea>
			</u-form-item>
			<u-form-item label="图片" prop="fileList1" borderBottom ref="item1">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="100"></u-upload>
			</u-form-item>
			<u-button @click="submit" type="primary">评论</u-button>
		</u--form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					mark: ''
				},
				fileList1: [],
				rules: {
					"mark": {
						type: 'string',
						required: true,
						message: '请填写评论内容',
						trigger: ['blur', 'change']
					}
				},
				house_id:''
			};
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].thumb)
					let item = this[`fileList${event.name}`][fileListLen]
					const data = JSON.parse(result);
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: data.data.path
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let that = this;
				const app = getApp().globalData.base_url;
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: app + '&map=applet_img_upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'image',
						success: (res) => {
							console.log(res.data)
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},
			submit() {
				let that = this;
				this.$refs.form1.validate().then(res => {
					that.http("applet_house_comment_index",{
						mark:that.formData.mark,
						imgs:that.fileList1,
						house_id:that.house_id
					}).then(res=>{
						if(res.ec == 200){
							uni.showToast({
								title:res.em,
								success() {
									uni.navigateBack({
										
									});
								}
							})
						}
					})
				}).catch(errors => {
					uni.$u.toast('请完善信息')
				})
			}
		},
		onLoad(option) {
			this.house_id = option.id
		}

	}
</script>

<style lang="scss">
	.content {
		width: 94%;
		margin: 0 auto;
	}
</style>
